<template>
  <div class="login-page">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="login-title">🔐后台管理系统</span>
      </div>
      <div class="login-form">
        <el-form :model="form" :rules="loginRules" ref="loginForm">
          <el-form-item prop="userName">
            <el-input type="text" v-model="form.userName" auto-complete="off" placeholder="请输入用户名">
              <template slot="prepend"><i style="font-size:20px" class="el-icon-user"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" v-model="form.password" auto-complete="off" placeholder="请输入密码">
              <template slot="prepend"><i style="font-size:20px" class="el-icon-key"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="width:100%;" type="primary" @click="handleLogin" :loading="loading">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      return {
        loading: false,
        form: {
          userName: '',
          password: ''
        },
        loginRules: {
          userName: [
            { required: true, message: '请输入账户', trigger: 'blur' },
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur'}
          ]
        },
        loginUrl: '/login', // 登录请求URL
        successRoute: '/main/home' // 登录成功后跳转的路由
      }
    },
    computed: {
      axios() {
        return this.$axios;
      },
      httpUrl() {
        return this.$httpUrl;
      }
    },
    methods: {
      async handleLogin() {
        try {
          await this.$refs.loginForm.validate();
          this.loading = true;

          const response = await this.axios.post(
            this.httpUrl + this.loginUrl,
            null,
            {
              params: {
                workNumber: this.form.userName,
                password: this.form.password
              }
            }
          );

          // 登录成功
          if (response.data.code === 200) {
            // 添加传值方法，向 Main 组件传递登录成功的用户名
            await this.$router.push(this.successRoute);
            // 将登录账号保存到本地存储
            localStorage.setItem('loggedInUser', this.form.userName);
          } else {
            console.log(response.data.msg);
            this.$message.error(response.data.msg);
          }
        } catch (error) {
          console.error(error);
          this.$message.error('登录失败，请重试！');
        } finally {
          this.loading = false;
        }
      }
    }
  }
</script>

<style scoped>
  .login-page{
    background-image: linear-gradient(180deg, #2af598 0%, #009efd 100%);
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .login-title{
    font-size: 20px;
  }

  .box-card {
    width: 375px;
  }

</style>
